﻿using System;
using System.Windows.Forms;

namespace Test
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private DateTime startTime;
        private DateTime picked;

        private void okButton_Click(object sender, EventArgs e)
        {
            DateTime timePicker = dateTimePicker.Value;
            //year, month, day, hour, minute, and second
            picked = new DateTime(timePicker.Year, timePicker.Month,
                timePicker.Day, hourTrackBar.Value, minuteTrackBar.Value,timePicker.Second);
            startTime = DateTime.Now;
            TimeSpan diff = startTime.Subtract(startTime);

            if (DateTime.Compare(picked, startTime) >= 0)
            {
                countDownTimer.Start();
            }
            else
            {
                MessageBox.Show("Picked Time Less Than Current Time", "Error",
                    MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void hourTrackBar_Scroll(object sender, EventArgs e)
        {
            hourLabel.Text =  hourTrackBar.Value.ToString();
        }

        private void minuteTrackBar_Scroll(object sender, EventArgs e)
        {
            minuteLabel.Text = minuteTrackBar.Value.ToString();
        }

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            hourLabel.Text = DateTime.Now.Hour.ToString() ;
            minuteLabel.Text = DateTime.Now.Minute.ToString();
            hourTrackBar.Value = DateTime.Now.Hour;
            minuteTrackBar.Value = DateTime.Now.Minute;
        }

        private void countDownTimer_Tick(object sender, EventArgs e)
        {
            DateTime currentTime = DateTime.Now;
            TimeSpan timeTillShutDown = picked.Subtract(currentTime);
            countDownLabel.Text = "ShutDown in " + timeTillShutDown.Hours.ToString() + 
                ":" + timeTillShutDown.Minutes.ToString() +
                ":" + timeTillShutDown.Seconds.ToString();

            if (timeTillShutDown.Hours == 0 && timeTillShutDown.Minutes == 0 && timeTillShutDown.Seconds == 0)
            {
                System.Diagnostics.Process.Start("shutdown", "/s /t 0");
            }
        }

        private void resetButton_Click(object sender, EventArgs e)
        {
            countDownTimer.Stop();
            hourLabel.Text = DateTime.Now.Hour.ToString();
            minuteLabel.Text = DateTime.Now.Minute.ToString();
            hourTrackBar.Value = DateTime.Now.Hour;
            minuteTrackBar.Value = DateTime.Now.Minute;
            countDownLabel.Text = "";
        }
       
    }
}
